﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Management_MedicalDirector_Quanlythuoc : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        tbMa.Enabled = false;
    }
    protected void btThem_Click(object sender, EventArgs e)
    {
        
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT * FROM tbTotalDrug WHERE IDDrug=" + dlQLthuoc.SelectedValue;
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataReader reader = cmd.ExecuteReader();
        if (!reader.Read())
        {
            dsTong.InsertParameters["IDDrug"].DefaultValue = dlQLthuoc.SelectedValue;
            dsTong.InsertParameters["Quantity"].DefaultValue = cbNumber.Text;
            dsTong.Insert();
        }
        else 
        {
            con.Close();
            con.Open();

            sql = "Update tbTotalDrug set Quantity=Quantity + @quantity where IDDrug=@id";
            cmd = new SqlCommand(sql, con);
            cmd.Parameters.AddWithValue("@id",dlQLthuoc.SelectedValue);
            cmd.Parameters.AddWithValue("@Quantity",cbNumber.Text);
            cmd.ExecuteNonQuery();

        }
        GridView1.DataBind();
    }
    protected void btThemThuoc_Click(object sender, EventArgs e)
    {
        dsThuoc.InsertParameters["Type"].DefaultValue = dlType.SelectedValue;
        dsThuoc.InsertParameters["Name"].DefaultValue = tbTen.Text;
        dsThuoc.InsertParameters["Desc"].DefaultValue = tbMota.Text;
        dsThuoc.InsertParameters["UnitType"].DefaultValue =tbDonvi.Text;
        dsThuoc.InsertParameters["UseMethod"].DefaultValue = tbSudung.Text;
        dsThuoc.InsertParameters["CostPerUnit"].DefaultValue = tbTile.Text;
        dsThuoc.Insert();
        GridView2.DataBind();
        tbTen.Text = "";
        tbMota.Text = "";
        tbDonvi.Text = "";
        tbSudung.Text = "";
        tbTile.Text = "";
    }
}